import os
import urllib.request
from lxml import etree

# 请求头伪装
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
                  '(KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0'
}

# 创建总目录
os.makedirs('前十页风景图', exist_ok=True)

for i in range(1, 11):  # 前十页
    page = f'_{i}' if i != 1 else ''
    url = f'https://sc.chinaz.com/tupian/fengjing{page}.html'
    print(f'正在爬取第{i}页：{url}')

    req = urllib.request.Request(url, headers=headers)
    response = urllib.request.urlopen(req).read().decode('utf-8')
    html = etree.HTML(response)

    # 创建当前页的子目录
    page_dir = f'./前十页风景图/第{i}页'
    os.makedirs(page_dir, exist_ok=True)

    for j in range(1, 41):  # 每页最多40张
        try:
            imgurl = html.xpath(f'/html/body/div[3]/div[2]/div[{j}]/img/@data-original')[0]
            imgname = imgurl.split('/')[-1]  # 取图片名
            save_path = os.path.join(page_dir, imgname)
            urllib.request.urlretrieve('https:'+imgurl, save_path)
            print(f'  下载成功: {imgname}')
        except Exception as e:
            print(f'  第{j}张下载失败: {e}')
